//
//  ToastManager.swift
//  StoreProject
//
//  Created by CYC on 1998/1/4.
//





import UIKit

class ToastManager: NSObject {
    
    static func makeToast(_ msg: String) -> Void {
        
        let text = String.init(format: "%@", msg)
        let BW = kScreenWidth - 20*2
        let size = text.boundingRect(with: .init(width: BW, height: CGFloat(MAXFLOAT)),
                                     options: .usesLineFragmentOrigin,
                                     attributes: [NSAttributedString.Key.font : kRFont(15)],
                                     context: nil);
        
        // 左右边距10 上下边距5
        let view = UIView.init(frame: CGRect.init(x: (kScreenWidth - size.width - 10*2)/2.0,
                                                  y: kNavHeight - 44 + kSpaceSize - kScreenHeight,
                                                  width: size.width + 10*2,
                                                  height: size.height + 5*2))
        view.backgroundColor = kRGBAColor(1, 16, 44, 0.92)
        view.c_radius(radius:10)
        kPManager.window!.addSubview(view)

        
        let lab = UILabel.init(frame: CGRect.init(x: 10,
                                                  y: 5,
                                                  width: size.width,
                                                  height: size.height))
        lab.textColor = .white
        lab.textAlignment = .center
        lab.numberOfLines = 0
        lab.font = kRFont(15)
        lab.text = text
        view.addSubview(lab)
        
        
        // 显示与隐藏
        UIView.animate(withDuration: 0.35) {
            
            view.transform = .init(translationX: 0, y: kScreenHeight)
            
        } completion: { (flag1) in
            
            // 根据字数控制显示时长
            var time: Double = 1.5
            if text.count < 10 {
                time = 1.5
            } else if text.count > 100 {
                time = 1.5 + 5.0
            } else {
                time = 1.5 + Double(text.count) * 0.05
            }
            DispatchQueue.main.asyncAfter(deadline: .now() + time) {
                
                UIView.animate(withDuration: 0.35) {
                    view.transform = .init(translationX: 0, y: 0)
                } completion: { (flag2) in
                    view.removeFromSuperview()
                }
            }
            
        }

        
        
        
    }
    
}
